/*************************************************************************
	> File Name: client.c
	> Author: 
	> Mail: 
	> Created Time: Fri 13 Jan 2017 04:11:53 AM PST
 ************************************************************************/

#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<signal.h>
#include"student.h"

#define BZ   100

int sockfd,t;
USER fir;
USER *msg = &fir;
INFO *infr;

void do_help()
{
    printf("|---------------------------------------------------|\n");
    printf("|---------------员工管理系统手册--------------------|\n");
    printf("|*******************普通用户************************|\n");
    printf("register ------> 登录服务器\n");
    printf("find ---------->  信息查询\n");
    printf("update -------->  信息修改\n");
    printf("repassword----->  密码修改\n");
    printf("quit ---------->    退出\n");
    printf("---------------------------------------------------|\n");
    printf("***************管理员添加权限**********************|\n");
    printf("adduser --------> 添加用户\n");
    printf("delete ---------> 删除用户\n");
    printf("|---------------->评级,修改工资\n");
    printf("|--------------------------------------------------|\n");
    return;
}

void do_quit()
{
    close(sockfd);
    exit(0);
}

void do_register()
{
    int  ret;
    char str[30] = {0};
    while(1)
    {
        printf("请输入用户名:\n");
        fgets(msg->name,sizeof(msg->name),stdin);
        if(strncmp(msg->name,"quit",4) == 0){
            break;
        }
        printf("请输密码:\n");
        fgets(msg->pwd,sizeof(msg->pwd),stdin);
        if(strncmp(msg->pwd,"quit",4) == 0){
            break;
        }
        ret = send(sockfd,msg,sizeof(*msg),0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");
            break;
        }
        bzero(str,sizeof(str));
        ret = recv(sockfd,str,2,0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");   
            break;
        }
        else if(strncmp(str,"N",1) == 0)
        {
            puts("用户不存在或密码错误或用户已上线!");
            break;
        }
        else if (strncmp(str,"C",1) == 0)
        {
            puts("登录成功！");
            t = 1;
            break;
        }
    }
    return;
}

void do_find()
{
    if(t == 0){
        puts("未登录！");
        return;
    }
    int ret;
    char buf[20] = {0};
    char buff[20] = {0};
    printf("请输入自己的用户名:\n");
    fgets(msg->name,sizeof(msg->name),stdin);
    if(strncmp(msg->name,"quit",4) == 0){
        return;
    }
    ret = send(sockfd,msg,sizeof(*msg),0);
    if(ret < 0){
        puts("未知错误！");
        exit(-1);
    }else if(ret == 0){
        puts("网络连接中断！");
        exit(-1);
    }
    bzero(buf,sizeof(buf));
    ret = recv(sockfd,buf,sizeof(buf),0);
    if(ret < 0){
        puts("未知错误！");
        exit(-1);
    }else if(ret == 0){
        puts("网络连接中断！");   
        exit(-1);
    }
    else if(strncmp(buf,"N",1) == 0)
    {
        puts("用户不存在或输入错误！");
        return;
    }
    else if(strncmp(buf,"Y",1) == 0)
    {
        puts("find!");
    }
        while(1)
        {
            bzero(buff,sizeof(buff));
            ret = recv(sockfd,buff,sizeof(buff),0);
            if(ret < 0)
            {
                perror("recv1");
                break;
            }
            if(ret == 0)
            {
                printf("连接中断!\n");
                break;   
            }
            if(strncmp(buff,"over",4) == 0)
            {
                break;  
            }
            printf("%s",buff);
            fflush(stdout);
        }
        puts("");
    return;
}

void do_update()
{
    if(t == 0){
        puts("未登录！");
        return;
    }

}

void do_repasswod()
{
    if(t == 0){
        puts("未登录！");
        return;
    }
    int ret;
    char buf[30] = {0};
    while(1)
    {
        printf("请输入自己的用户名:\n");
        fgets(msg->name,sizeof(msg->name),stdin);
        if(strncmp(msg->name,"quit",4) == 0){
            break;
        }
        printf("请输入新密码:\n");
        fgets(msg->pwd,sizeof(msg->pwd),stdin);
        if(strncmp(msg->pwd,"quit",4) == 0){
            break;
        }
        ret = send(sockfd,msg,sizeof(*msg),0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");
            break;
        }
        bzero(buf,sizeof(buf));
        ret = recv(sockfd,buf,2,0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");   
            break;
        }
        else if(strncmp(buf,"C",1) == 0)
        {
            puts("用户不存在！");
            break;
        }else if (strncmp(buf,"Y",1) == 0)
        {
            puts("密码修改成功！");
            break;
        }
        return;
    }
}

void do_adduser()
{
    int  ret;
    char str[30] = {0};
    if(t == 0){
        puts("未登录！");
        return;
    }
    while(1)
    {
        printf("请输入需要添加的用户名:\n");
        fgets(msg->name,sizeof(msg->name),stdin);
        if(strncmp(msg->name,"quit",4) == 0){
            break;
        }
        printf("请输入密码:\n");
        fgets(msg->pwd,sizeof(msg->pwd),stdin);
        if(strncmp(msg->pwd,"quit",4) == 0){
            break;
        }
        ret = send(sockfd,msg,sizeof(*msg),0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");
            break;
        }
        bzero(str,sizeof(str));
        ret = recv(sockfd,str,2,0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");   
            break;
        }
        else if(strncmp(str,"N",1) == 0)
        {
            puts("您没有权限!");
            break;
        }else if (strncmp(str,"C",1) == 0)
        {
            puts("该用户已存在，请勿重复添加！");
            break;
        }
        else if(strncmp(str,"Y",1) == 0)
        {
            puts("添加成功,请尽快完善用户详细信息!");
            ret = recv(sockfd,msg,sizeof(*msg),0);
            printf("user:%s  pwd:%s\n",msg->name,msg->pwd);
            break;
        }
    }
    return;
} 

void do_delete()
{
    int ret;
    char str[30] = {0};
    if(t == 1){
        puts("未登录！");
        return;
    }
    while(1)
    {
        printf("请输入需要删除的用户名:\n");
        fgets(msg->name,sizeof(msg->name),stdin);
        if(strncmp(msg->name,"quit",4) == 0){
            break;
        }
        ret = send(sockfd,msg,sizeof(*msg),0);
        if(ret < 0){
            puts("未知错误！");
            break;
        }else if(ret == 0){
            puts("网络连接中断！");
            break;
        }
        bzero(str,sizeof(str));
        ret = recv(sockfd,str,2,0);
        if(ret < 0)
        {
            puts("未知错误");
            break;
        }
        else if(ret == 0)
        {
            puts("网络连接中断");
            break;
        }
        else if(strncmp(str,"N",1) == 0)
        {
            puts("您没有权限!");
            break;
        }else if (strncmp(str,"C",1) == 0)
        {
            puts("用户不存在！");
            break;
        }else if(strncmp(str,"Y",1) == 0)
        {
            puts("删除成功！");
            break;
        }
    }
    return;
}

int main()
{
    int ret;
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        perror("connect0");
        return -1;
    }
    struct sockaddr_in servaddr;
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8888);
    servaddr.sin_addr.s_addr = inet_addr("192.168.0.122");
    if(0 > connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))
    {
        perror("connect");
        return -1;
    }
    printf("请输入'help'命令查看使用手册.\n");
    while(1)
    {
        bzero(msg,sizeof(*msg));
        printf("-->$");
        fgets(msg->infor,sizeof(msg->infor),stdin);
        msg->infor[strlen(msg->infor)-1] = '\0';

        if(strncmp(msg->infor,"help",4) == 0)
        do_help();
        else if(strncmp(msg->infor,"quit",4) == 0)
        do_quit();
        else if(strncmp(msg->infor,"register",8) == 0)
        do_register();
        else if(strncmp(msg->infor,"find",4) == 0)
        do_find();
        else if(strncmp(msg->infor,"update",6) == 0)
        do_update();
        else if(strncmp(msg->infor,"repasswod",9) == 0)
        do_repasswod();
        else if(strncmp(msg->infor,"adduser",7) == 0)
        do_adduser();
        else if(strncmp(msg->infor,"delete",6) == 0)
        do_delete();
        else if(strncmp(msg->infor,"\0",1) == 0){
            continue;
        }
        else
        {
            fprintf(stderr,"命令错误！\n");
        }
    }
    return 0;
}

















